﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zero.Code;
using Zero.Model;

namespace Zero.DAL
{

    public class StudentManagementdal
    {
        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <returns></returns>
        public List<TreeSelectModel> GetList()
        {
            var treeList = new List<TreeSelectModel>();
            using (var db = new zerodbEntities())
            {
                var data = (from c in db.classes
                            select c).ToList();
                foreach (classes item in data)
                {
                    TreeSelectModel treeModel = new TreeSelectModel();
                    treeModel.id = item.C_Id.ToString();
                    treeModel.text = item.C_Name;
                    treeModel.parentId = item.C_ParentId.ToString();
                    treeModel.data = item;
                    treeList.Add(treeModel);
                }
            }
            return treeList;
        }
        /// <summary>
        /// 获取年级列表
        /// </summary>
        /// <returns></returns>
        public List<TreeGridModel> GetTreeGridList()
        {
            var treeList = new List<TreeGridModel>();
            using (var db = new zerodbEntities())
            {
                /*var data = (from s in db.systemmanage
                           select s).ToList();--!@#$这样获取的data值会乱*/
                string sql = string.Format(@"select * from systemmanage");
                var data = db.Database.SqlQuery<systemmanage>(sql).ToList();
                foreach (var item in data)
                {
                    TreeGridModel treeModel_s = new TreeGridModel();
                    bool hasChildren_s = data.Count(t => t.ParentId.ToString() == item.Id.ToString()) == 0 ? false : true;
                    treeModel_s.id = item.Id.ToString();
                    treeModel_s.isLeaf = hasChildren_s;
                    treeModel_s.parentId = item.ParentId.ToString();
                    treeModel_s.expanded = hasChildren_s;
                    treeModel_s.entityJson = item.ToJson();
                    treeModel_s.layer = item.Layers.ToInt();
                    treeList.Add(treeModel_s);
                }
            }
            return treeList;
        }

        /// <summary>
        /// 根据实体ID动态返回查询的实体的信息
        /// </summary>
        /// <returns></returns>
        public List<systemmanage> GetFormJson(string keyValue)
        {
            using (var db = new zerodbEntities())
            {
                var data = (from s in db.systemmanage
                            where s.Id == keyValue
                            select s).ToList();
                return data;
            }
        }
        /*public string GetTreeGrid()
        {
            using (var db=new zerodbEntities())
            {
                var Tree=from g in db.grades
                         join c in db.classes on g.G_Id equals c.C_ParentId
                         select new
                         {
                             Id=
                         }
            }
        }*/
    }
}
